package com.atguigu.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.OrderSettingDao;
import com.atguigu.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> orderSettings) {
        for (OrderSetting orderSetting : orderSettings) {
            Long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
            if(count>0){
                orderSettingDao.edit(orderSetting);
            }else{
                orderSettingDao.add(orderSetting);
            }

        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(date);
        List<Map> mapList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map map=new HashMap();
            map.put("date",orderSetting.getOrderDate().getDate());//天
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        OrderSetting orderSettingdb = orderSettingDao.getOrderSettingByOrderDate(orderSetting.getOrderDate());
        if(orderSettingdb==null){
            orderSettingDao.add(orderSetting);
        }else{
            if(orderSetting.getNumber()<orderSettingdb.getReservations()){
                throw new RuntimeException("不允许修改");
            }else{
                orderSettingDao.edit(orderSetting);
            }
        }
    }
}
